package com.cax.module.file.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;
import com.cax.module.file.service.FileService;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import com.cax.module.file.domain.vo.FileVO;
import com.cax.common.core.domain.Result;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import org.springframework.web.multipart.MultipartFile;

/**
 * 文件
 * @author liangjian
 * @date 2023-11-24 16:52:24
 */
@Slf4j
@RestController
@Api(tags = "文件")
@RequestMapping("/file")
@Validated
public class FileController {
    /**
     * 文件表 Service
     */
    @Autowired
    public FileService fileService;

	@GetMapping("test")
	@ApiOperation(value = "测试", notes = "")
	@ApiImplicitParams({
	})
	public Result test(
	) throws IOException {
		fileService.test();
		return Result.ok();
	}

	@PostMapping("upload/test")
	@ApiOperation(value = "上传 测试", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "file", value = "参数file一定是对应表单中文件input的name属性", required = true, dataType = "MultipartFile")
	})
	public Result<FileVO> uploadTest(
			@RequestParam(name = "file", required = true) MultipartFile file
	) throws IOException {
		FileVO result = fileService.uploadTest(file);
		return Result.ok(result);
	}

	@GetMapping("downLoad/test")
	@ApiOperation(value = "下载 测试", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "fileId", value = "文件id", required = true, dataType = "Long"),
			@ApiImplicitParam(name = "fileName", value = "指定下载后的文件名称（不包含后缀 后缀根据文件上传的后缀） 如果不指定 使用文件上传时的名称", required = false, dataType = "String")
	})
	public void downLoadTest(
			HttpServletResponse response,
			@RequestParam(name = "fileId") Long fileId,
			@RequestParam(name = "fileName",required = false) String fileName
	) {
		fileService.fileDownload(response,fileId,fileName);
	}

	@GetMapping("del/test")
	@ApiOperation(value = "删除无效文件", notes = "")
	public Result del() {
		fileService.del();
		return Result.ok();
	}
}

